package org.idontknow.prey;

import org.idontknow.prey.species.*;
import org.idontknow.prey.exception.*;
//import org.idontknow.prey.species.Tiger;
//import org.idontknow.prey.species.Rabbit;
//import org.idontknow.prey.species.Dinosaur;
//import org.idontknow.prey.species.Grass;
//import org.idontknow.prey.species.Goat;
//import org.idontknow.prey.species.Bear;
//import org.idontknow.prey.species.Fish;
//import org.idontknow.prey.species.Wolf;

class TheWorld {

  public enum Find {eats, eatenBy, all, indirectlyEats };

  public static void main(String[] args) {

    init();
    testAll();

/*    print(Find.eats, "Fish");
    print(Find.eats, "Tiger");
    print(Find.all);
    print(Find.indirectlyEats, "Dinosaur");
    print(Find.eatenBy, "Grass");
    print(Find.eatenBy, "Dinosaur");
    print(Find.indirectlyEats, "Rabbit");
    print(Find.indirectlyEats, "Grass");
    print(Find.eatenBy, "Wolf");*/

    //    Species predator = new Tiger();
    //    System.out.println(predator.getName());
  }

  public static void init() {

    System.out.println("Initializing the world...");

    God.eats("Tiger", "Rabbit");
    God.eats("Rabbit", "Grass");
    God.eats("Dinosaur", "Tiger");
    God.eats("Dinosaur", "Grass");
    God.eats("Dinosaur", "Rabbit");
    God.eats("Goat", "Grass");
    God.eats("Tiger", "Goat");
    God.eats("Dinosaur", "Goat");
    God.eats("Bear", "Rabbit");
    God.eats("Bear", "Fish");
    God.eats("Wolf", "Goat");
    God.eats("Wolf", "Rabbit");
    God.eats("Tiger", "Wolf");
    God.eats("Dinosaur", "Wolf");

    System.out.println("World initialization finished! We have a world as follows : ");
    
    System.out.println("----------------------------------------------------------");
    try{
      for(Species obj: God.speciesByWeight()) {
        try{
          for(Species prey: God.whoIsEatenBy(obj.getName())) {
            System.out.println(obj.getName() + " --> " + prey.getName());
          }
        }catch(BadLuckException e) {
          System.out.println(obj.getName() + " --> X");
          continue;
        }
      }
    }catch(EmptyWorldException e) {
      System.out.println("Shit happens!!!!\n\n");
    }
    System.out.println("----------------------------------------------------------\n");
  }

  public static void print(Find findType) throws BadUsageException {
    if(findType != Find.all)
      throw new BadUsageException();
    else {
      try{
        Species[] allSpecies = God.speciesByWeight();
        System.out.println("Now, we have these " + allSpecies.length + " species in the world : ");
        for(Species any: allSpecies) 
          System.out.println("  " + any.getName() + " : " + any.getWeight());
        System.out.println();
      }catch(EmptyWorldException e) {
        System.out.println("Wow! there's nothing in this totally clean world!!!");
      }
    }
  }

  public static void print(Find findType, String name) {
    try {
      switch(findType) {
        case eats:
          System.out.println(name + " eats : ");
          for(Species prey: God.whoIsEatenBy(name))
            System.out.println("  " + prey.getName());
          break;
        case eatenBy:
          System.out.println(name + " is eaten by : ");
          for(Species predator: God.whoEats(name))
            System.out.println("  " + predator.getName());
          break;
        case indirectlyEats:
          System.out.println(name + " indirectly eats : ");
          for(Species any: God.whoIsIndirectlyEatenBy(name))
            System.out.println("  " + any.getName());
          break;
      }
      System.out.println();
    }catch(GoodLuckException e) {
      System.out.println("  Good Luck! nobody eats me...\n");
    }catch(BadLuckException e) {
      System.out.println("  Bad Luck! nobody for me to eat...\n");
    }catch(SomeLuckException e) {
      System.out.println("  Not that many somebody to eat...\n");
    }
  }

  public static void testAll() {
    try{
      print(Find.all);
    }catch(BadUsageException e) {
      System.out.println("Caught u!! Bad usage of method: public static void print(Find);\n");
    }

    try{
      for(Species spec: God.speciesByWeight()) {
        print(Find.eats, spec.getName());
        print(Find.eatenBy, spec.getName());
        print(Find.indirectlyEats, spec.getName());
      }
    }catch(Exception e) {
      System.out.println("Shit happens!!!\n\n");
    }
  }
}
